/** * Created by rubin on 2020/6/4. */

<template>
  <main>
    <bg />
    <div class="login-content">
      <div class="content">
        <div class="form sign-in">
          <h2>{{ title }} 登录</h2>
          <label :class="focus.username || loginForm.username ? 'on' : ''">
            <div class="placeholder">
              <span>
                <span>用</span>
                <span>户</span>
                <span>名</span>
              </span>
            </div>
            <i class="el-icon-user"></i>
            <input
              type="text"
              @keyup.enter="doLogin"
              v-model="loginForm.username"
              ref="username"
              @focus.self="focus.username = true"
              @blur.self="focus.username = loginForm.username ? true : false"
            />
          </label>
          <label :class="focus.password || loginForm.password ? 'on' : ''">
            <div class="placeholder">
              <span>
                <span>密</span>
                <span>码</span>
              </span>
            </div>
            <i class="el-icon-lock"></i>
            <input
              type="password"
              @keyup.enter="doLogin"
              v-model="loginForm.password"
              @focus.self="focus.password = true"
              @blur.self="focus.password = loginForm.password ? true : false"
            />
          </label>
          <p class="forgot-pass">
            <a href="javascript:" @click="goForget"> 忘记密码？ </a>
          </p>
          <button
            :loading="loading"
            @click="doLogin"
            type="button"
            class="styled-button"
          >
            <span class="styled-button__real-text-holder">
              <span class="styled-button__real-text">{{ btnText }}</span>
              <span class="styled-button__moving-block face">
                <span class="styled-button__text-holder">
                  <span class="styled-button__text">{{ btnText }}</span>
                </span> </span
              ><span class="styled-button__moving-block back">
                <span class="styled-button__text-holder">
                  <span class="styled-button__text">{{ btnText }}</span>
                </span>
              </span>
            </span>
          </button>
          <div class="sub-cont" @click="goRegister">
            还没有账号？<span>注册一个</span>
          </div>
        </div>
      </div>
      <div style="display: none" id="cube-template">
        <div class="cube">
          <div class="shadow"></div>
          <div class="sides">
            <div class="back"></div>
            <div class="top"></div>
            <div class="left"></div>
            <div class="front"></div>
            <div class="right"></div>
            <div class="bottom"></div>
          </div>
        </div>
      </div>
    </div>
  </main>
</template>

<script>
import panUtil from '../../utils/common'
import userService from '../../api/user'
import { setToken } from '../../utils/cookie'
import { mapMutations } from 'vuex'
import bg from '@/layout/bg'
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
      focus: {
        username: false,
        password: false,
      },
      loading: false,
      btnText: '登 录',
      title: import.meta.env.VITE_TITLE,
    }
  },
  components: {
    bg,
  },
  methods: {
    ...mapMutations({
      setParentId: 'file/setFileParentId',
      setDefaultParentId: 'file/setFileDefaultParentId',
      setDefaultParentFilename: 'file/setDefaultParentFilename',
      setUsername: 'user/setUsername',
    }),
    goRegister() {
      this.$router.push({ name: 'Register' })
    },
    goForget() {
      this.$router.push({ name: 'Forget' })
    },
    doLogin() {
      let _this = this
      if (_this.checkLoginForm()) {
        _this.loading = true
        userService.login(
          _this.loginForm,
          (res) => {
            setToken(res.data)
            userService.info(
              (res) => {
                _this.setParentId(res.data.rootFileId)
                _this.setDefaultParentId(res.data.rootFileId)
                _this.setDefaultParentFilename(res.data.rootFilename)
                _this.setUsername(res.data.username)
                _this.$router.push({ name: 'Index' })
              },
              (res) => {
                _this.$message.error(res.message)
              }
            )
          },
          (res) => {
            this.loginForm.password = ''
            _this.$message.error(res.message)
            _this.loading = false
          }
        )
      }
    },
    checkLoginForm() {
      if (!panUtil.checkUsername(this.loginForm.username)) {
        this.$message.error('请输入6-16位只包含数字和字母的用户名')
        return false
      }
      if (!panUtil.checkPassword(this.loginForm.password)) {
        this.$message.error('请输入8-16位的密码')
        return false
      }
      return true
    },
  },
  mounted() {
    this.$refs.username.focus()
    // this.$nextTick(() => {
    //   // bg()
    // })
  },
  watch: {},
}
</script>

<style lang="scss" scoped>
*,
*:before,
*:after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

input {
  border: none;
  outline: none;
  background: none;
  font-family: 'Open Sans', Helvetica, Arial, sans-serif;
}
svg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
}
.login-content {
  min-height: 100vh;
  .content {
    overflow: hidden;
    position: absolute;
    left: 50%;
    top: 50%;
    // width: 900px;
    // height: 550px;
    // margin: -300px 0 0 -450px;
    transform: translate(-50%, -50%);
    background: #fff;
    .form {
      position: relative;
      // width: 640px;
      height: 100%;
      transition: -webkit-transform 0.6s ease-in-out;
      transition: transform 0.6s ease-in-out;
      transition: transform 0.6s ease-in-out, -webkit-transform 0.6s ease-in-out;
      padding: 50px;

      h2 {
        width: 100%;
        font-size: 26px;
        text-align: center;
      }

      label {
        display: block;
        width: 260px;
        margin: 25px auto 0;
        border: 1px solid rgba(0, 0, 0, 0.4);
        border-radius: 3px;
        position: relative;
        i {
          position: absolute;
          left: 16px;
          top: 50%;
          visibility: visible;
          transform: translateY(-50%) scale(1);
          transition: all 0.4s;
        }
        .placeholder {
          position: absolute;
          left: 0;
          top: 50%;
          height: 100%;
          transform: translateY(-50%);
          padding-left: 45px;
          display: flex;
          align-items: center;
          & > span {
            display: inline-block;
            perspective: 500px;
            span {
              display: inline-block;
              font-size: 14px;
              color: #909399;
              animation: letterAnimOut 0.5s ease forwards;

              // @for $i from 1 through 3 {
              //   &:nth-child(#{$i}) {
              //     animation-delay: $i * 0.0.5s;
              //   }
              // }
            }
          }
        }

        &.on {
          i {
            transform: translateY(-50%) scale(0.1);
            visibility: hidden;
            opacity: 0;
          }
          .placeholder {
            span {
              span {
                animation: letterAnimIn 0.5s ease forwards;
                // transform: translate(-30px, -21px);
                // text-shadow: 0 0 5px;
                @for $i from 1 through 3 {
                  &:nth-child(#{$i}) {
                    animation-delay: $i * 0.1s;
                  }
                }
              }
            }
          }
        }

        input {
          display: block;
          width: 100%;
          // margin-top: 5px;
          padding: 12px 15px;
          font-size: 16px;
          // border-bottom: 1px solid rgba(0, 0, 0, 0.4);
        }
      }
    }
  }
}
@keyframes letterAnimIn {
  0% {
    -webkit-transform: translate(0, 0);
    transform: translate(0, 0);
  }
  25% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    color: red;
  }
  45% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    opacity: 0;
    color: red;
  }
  55% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    opacity: 0;
  }
  56% {
    -webkit-transform: translate(-30px, -21px);
    transform: translate(-30px, -21px);
    opacity: 0;
    color: #00ff6b;
  }
  76% {
    color: #00ff6b;
    opacity: 1;
    -webkit-transform: translate(-30px, -21px);
    transform: translate(-30px, -21px);
  }
  100% {
    -webkit-transform: translate(-30px, -21px);
    transform: translate(-30px, -21px);
    background-color: #fff;
    opacity: 1;
  }
}
@keyframes letterAnimOut {
  0% {
    -webkit-transform: translate(-30px, -21px);
    transform: translate(-30px, -21px);
    opacity: 1;
  }
  25% {
    -webkit-transform: translate(-30px, -40px);
    transform: translate(-30px, -40px);
    opacity: 0;
  }
  45% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    opacity: 0;
  }
  55% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    opacity: 0;
    color: red;
  }
  56% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    color: red;
  }
  100% {
    -webkit-transform: translate(0, 0);
    transform: translate(0, 0);
  }
}

.styled-button {
  appearance: none;
  user-select: none;
  cursor: pointer;
  font-size: 14px;
  width: 100%;
  padding: 15px;
  outline: none;
  background: none;
  position: relative;
  color: rgba($color: #fff, $alpha: 0.8);
  border-radius: 3px;
  margin-bottom: 25px;
  border: none;
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 0.1em;
  background: #65c6cf;
  -webkit-transition: all 0.3s ease;
  transition: all 0.3s ease;
  overflow: hidden;
  margin-top: 20px;
}

.styled-button__real-text {
  color: transparent;
  display: inline-block;
}
.styled-button__text-holder {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
.styled-button__moving-block {
  -webkit-transition: all 0.3s ease;
  transition: all 0.3s ease;
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
}
.styled-button__moving-block.back {
  color: white;
  -webkit-transform: translateX(-100%);
  transform: translateX(-100%);
}
.styled-button__moving-block.back .styled-button__text-holder {
  -webkit-transform: translateX(100%);
  transform: translateX(100%);
}
.styled-button:hover,
.styled-button:active {
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
  background: #65c6cf;
}
.styled-button:hover .face,
.styled-button:active .face {
  -webkit-transform: translateX(100%);
  transform: translateX(100%);
}
.styled-button:hover .face .styled-button__text-holder,
.styled-button:active .face .styled-button__text-holder {
  -webkit-transform: translateX(-100%);
  transform: translateX(-100%);
}
.styled-button:hover .back,
.styled-button:active .back {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}
.styled-button:hover .back .styled-button__text-holder,
.styled-button:active .back .styled-button__text-holder {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}
.styled-button:active {
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}

.forgot-pass {
  margin-top: 15px;
  text-align: right;
  font-size: 12px;
  a {
    color: #999;
  }
}
.sub-cont {
  font-size: 12px;
  color: #999;
  text-align: center;
  cursor: pointer;
  span {
    color: #65c6cf;
  }
}
</style>
